package lib.bone.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.system.dao.SysMenuDAO;
import lib.bone.system.dao.SysRoleDAO;
import lib.bone.system.dao.SysRoleMenuDAO;
import lib.bone.system.domain.entity.SysMenu;
import lib.bone.system.domain.entity.SysRole;
import lib.bone.system.domain.entity.SysRoleMenu;
import lib.bone.system.domain.vo.MenuVO;
import lib.bone.system.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author : kindear
 * @Date : 2021-12-13
 */
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
    @Autowired
    SysRoleDAO sysRoleDAO;
    @Autowired
    SysMenuDAO sysMenuDAO;
    @Autowired
    SysRoleMenuDAO sysRoleMenuDAO;

    @Override
    public ResponseDTO addRoleMenu(Long roleId, Long menuId) {
        /**
         * 检测是否存在
         */
        Optional<SysRole> roleOp = sysRoleDAO.findById(roleId);
        if(!roleOp.isPresent()){
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+roleId+"]对应角色不存在");
        }
        Optional<SysMenu> menuOp = sysMenuDAO.findById(menuId);
        if(!menuOp.isPresent()){
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+menuId+"]对应菜单不存在");
        }
        /**
         * 查询记录中是否已经存在
         */
        Optional<SysRoleMenu> sysRoleMenuOp = sysRoleMenuDAO.findByRoleIdAndMenuId(roleId,menuId);
        if(sysRoleMenuOp.isPresent()){
            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"禁止重复创建");
        }
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleMenu.setRoleId(roleId);
        sysRoleMenu.setMenuId(menuId);
        //写入记录
        return ResponseDTO.succData(sysRoleMenuDAO.save(sysRoleMenu));
    }
    @Override
    public ResponseDTO getRoleMenus(Long roleId){
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuDAO.findByRoleId(roleId);
        List<Long> menuIds = new ArrayList<>();
        sysRoleMenus.forEach(sysRoleMenu -> {
            menuIds.add(sysRoleMenu.getMenuId());
        });
        List<SysMenu> menus = sysMenuDAO.findByIdIn(menuIds);
        List<MenuVO> menuVOS = new ArrayList<>();
        menus.forEach(menu->{
            MenuVO menuVO = new MenuVO();
            BeanUtil.copyProperties(menu,menuVO, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            menuVOS.add(menuVO);
        });
        return ResponseDTO.succData(menuVOS);
    }
    @Override
    @Transactional
    public ResponseDTO batchAddRoleMenu(Long roleId, List<Long> menuIds){

        //清理旧数据
        sysRoleMenuDAO.deleteByRoleId(roleId);
        List<SysRoleMenu> addTask = new ArrayList<>();
        for (Long menuId : menuIds) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(menuId);
            addTask.add(sysRoleMenu);
        }
        return ResponseDTO.succData(sysRoleMenuDAO.saveAll(addTask));
    }

    public void deleteByRoleId(Long roleId){
         sysRoleMenuDAO.deleteByRoleId(roleId);
    }
}
